package drive.RemakeController;



import drive.Model.Second_Img;
import drive.Model.Secondhanditem;
import drive.Model.TransactionOrder;
import drive.RemakeService.ServiceInterface.MarketItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/market-item")
public class MarketItemController {

    @Autowired
    private MarketItemService marketItemService;

//    @PostMapping("/publish")
//    public String publishMarketItem(@RequestBody Secondhanditem item, @RequestParam String img_url) {
//        return marketItemService.publishMarketItem(item, img_url);
//    }

    @PostMapping("/publish")
    public String publishMarketItem_test(@RequestBody Second_Img secondImg){
        System.out.println(secondImg);
        System.out.println("发送成功");
        return marketItemService.publishMarketItem(secondImg.getSecondhanditem(),secondImg.getImage());
    }

    @GetMapping("/browse")
    public String browseMarketItems() {
        return marketItemService.browseMarketItems();
    }

    @PostMapping("/purchase")
    public String purchaseMarketItem(@RequestBody TransactionOrder transactionOrder) {
        return marketItemService.purchaseMarketItem(transactionOrder);
    }

    @PostMapping("/evaluate")
    public String evaluateMarketTransaction(@RequestBody TransactionOrder transactionOrder) {
        return marketItemService.evaluateMarketTransaction(transactionOrder);
    }

    @PostMapping("/report")
    public String reportMarketItem(@RequestHeader("token") String token, @RequestParam Integer itemID, @RequestParam String reason) {
        return marketItemService.reportMarketItem(token, itemID, reason);
    }

    /**
     * 根据时间分页查询商品
     *
     * @param start_time 开始时间
     * @param end_time   结束时间
     * @param pageNum    页码
     * @param pageSize   每页大小
     * @return 包含商品列表和分页信息的JSON字符串
     */
    @GetMapping("/pageByTime")
    public String getItemsByTime(
            @RequestParam String start_time,
            @RequestParam String end_time,
            @RequestParam int pageNum,
            @RequestParam int pageSize) {
        return marketItemService.getItemsByTime(start_time, end_time, pageNum, pageSize);
    }

    /**
     * 根据商品ID获取商品的第一张图片
     * @param itemID 商品ID
     * @return 包含图片信息的JSON字符串
     */
    @GetMapping("/firstimage")
    public String getFirstImageByItemID(@RequestParam int itemID) {
        return marketItemService.getFirstImageByItemID(itemID);
    }

    @GetMapping("/image")
    public String getImageByItemID(@RequestParam int itemID) {
        return marketItemService.getImageByItemID(itemID);
    }

    /**
     * 通过关键词检索商品
     * @param keyword 关键词
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 包含商品列表和分页信息的JSON字符串
     */
    @GetMapping("/search")
    public String searchItems(
            @RequestParam String keyword,
            @RequestParam int pageNum,
            @RequestParam int pageSize) {
        return marketItemService.searchItems(keyword, pageNum, pageSize);
    }

    /**
     * 根据商品ID创建会话
     * @param user1ID 用户1的ID
     * @param user2ID 用户2的ID
     * @param itemID 商品ID
     * @return 包含会话信息的JSON字符串
     */
    @PostMapping("/create")
    public String createChatSessionByItem(
            @RequestParam int user1ID,
            @RequestParam int user2ID,
            @RequestParam int itemID) {
        return marketItemService.createChatSessionByItem(user1ID, user2ID, itemID);
    }

    /**
     * 发送消息
     * @param chatRoomId 聊天室ID
     * @param senderId 发送者ID
     * @param content 消息内容
     * @return 操作结果的JSON字符串
     */
    @PostMapping("/send")
    public String sendMessage(
            @RequestParam int chatRoomId,
            @RequestParam int senderId,
            @RequestParam String content) {
        return marketItemService.sendMessage(chatRoomId, senderId, content);
    }

    /**
     * 收藏商品
     * @param userId 用户ID
     * @param itemId 商品ID
     * @return 操作结果的JSON字符串
     */
    @PostMapping("/item")
    public String favoriteItem(@RequestParam int userId, @RequestParam int itemId) {
        return marketItemService.favoriteItem(userId, itemId);
    }



}